(:
  A procedure to select the contents of the "textboard"
:)
declare boundary-space strip;
import module namespace
    jj = "http://kumquat.emse.fr/utilitaires"
    at "jjutils.xq" ;
<display>{
let $t := $arguments[1]
let $d := doc($t)/trace/(event[textboard] | event[tokentextboard/text])
for $e at $p in $d
let $prevelem := $e/preceding-sibling::event[(textboard|tokentextboard)/text][1]
let $thisnumchar := fn:string-length($e//text)
let $prevnumchar := fn:string-length($prevelem//text)        	
let $diffnumchar := $thisnumchar - $prevnumchar

return
  <item>{
    <info name="type">textboard</info> ,
    <info name="time">{ $e/time }</info>,
    <info name="src-anchor">{
      <anchor>{
        <doc>{ $t }</doc>,
        <path>{jj:build-Path($e)}</path>
      }</anchor>
    }</info>,
    <info name="user">{string($e/@user)}</info>,
    <info name="room">{string($e/@room)}</info>,
    <info name="content">{
       <text color="darkred">{
	let $pos := xs:integer($e//text/@caret) + 1
	let $contextsize := 3
	let $before := fn:substring($e//text,1,$pos)
	let $split := fn:tokenize($before,"\s")
	let $context := fn:string-join($split[position() ge (last()-$contextsize)], " ")
	return if ($diffnumchar < 0)
		then (-$diffnumchar, "chars removed near ",$context)
		else ($diffnumchar, "chars added near ",$context)
       }</text> 
    }</info>,
    <info name="character-difference">{$diffnumchar}</info>,
    <info name="currenttext">{string($e//text)}</info>,
    <info name="cursorposition">{string($e//text/@caret)}</info>
  }</item>
}</display>
